package com.itheima.bos.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.itheima.bos.domain.base.Courier;
import com.itheima.bos.domain.base.Standard;
import com.itheima.bos.service.base.CourierService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Controller
@ParentPackage(value="json-default")
@Namespace("/")
@Scope("prototype")
public class CourierAction extends ActionSupport implements ModelDriven<Courier>{
	
	private Courier courier = new Courier();
	@Override
	public Courier getModel() {
		return courier;
	}
	@Autowired
	private CourierService courierService;
	@Action(value="courier_save",
			results={@Result(name="success",location="./pages/base/courier.html",type="redirect")})
	public String save(){
		courierService.save(courier);
		return SUCCESS;
	}
	//条件查询
	private Integer page;
	private Integer rows;
	
	public void setPage(Integer page) {
		this.page = page;
	}
	public void setRows(Integer rows) {
		this.rows = rows;
	}
	@Action(value="courier_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		//封装 pageable
		Pageable pageable = new PageRequest(page-1, rows);
		//封装条件查询对象 Specification
		Specification<Courier> specification = new Specification<Courier>(){
			//root 用于获取属性字段，criteriaQuery可以用于简单条件查询，criteriaBuilder用于复杂条件查询
			@Override                                                                    //cb 查询语句构造器
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				//简单单表查询
				if(StringUtils.isNotBlank(courier.getCourierNum())){
					//  predicate 查询语句
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class), 
							courier.getCourierNum());
					list.add(p1);
				}
				if(StringUtils.isNotBlank(courier.getCompany())){
					Predicate p2 = cb.equal(root.get("company").as(String.class), 
						"%"	+ courier.getCompany()+"%");
					list.add(p2);
				}
				if(StringUtils.isNotBlank(courier.getType())){
					Predicate p3 = cb.equal(root.get("type").as(String.class), 
							courier.getType());
					list.add(p3);
				}
				//多表查询
				Join<Courier,Standard> standardJoin=root.join("standard",
						JoinType.LEFT);
				if( courier.getStandard()!=null &&
						StringUtils.isNotBlank(courier.getStandard().
								getName())){
					Predicate p4 = cb.like(
							standardJoin.get("name").as(String.class),
							"%" + courier.getStandard().getName()+"%");
					list.add(p4);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		//调用service 传入pageable
		Page<Courier> page = courierService.findPageData(specification, pageable);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", page.getTotalElements());
		map.put("rows", page.getContent());
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	
	private String ids;
	
	public void setIds(String ids) {
		this.ids = ids;
	}
	@Action(value="courier_delBatch",results={@Result(name="success",
			location="./pages/base/courier.html",type="redirect")})
	public String delBatch(){
		String[] idsArray = ids.split(",");
		courierService.delBatch(idsArray);
		return SUCCESS;
	}
	//查询没有关联的快递员
	@Action(value = "courier_findnoassociation",results={@Result(name="success",type="json")})
	public String findNoAssociation(){
		List<Courier> list=courierService.findNoAssociation();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
}
